//
//  TTEstimateDetailTopView.m
//  TaoTuo
//
//  Created by LG on 2020/8/13.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTEstimateDetailTopView.h"
#import "TTRecyclingValuationViewController.h"
#import "LLDateUtilis.h"
#import <UIView+TYAlertView.h>

@interface TTEstimateDetailTopView ()
{
    NSInteger hour;
    NSInteger minute;
    NSInteger second;
}

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) YYLabel *contentLabel;
@property (nonatomic, strong) UIButton *timeBtn;

@property (nonatomic, strong) TTEstimateDetailModel *model;

@end

@implementation TTEstimateDetailTopView

+ (instancetype)createTopViewWithModel:(TTEstimateDetailModel *)model
{
    TTEstimateDetailTopView *view = [[self alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 100*kScale)];
    view.backgroundColor = RGB(242, 233, 234);
    view.model = model;
    
    [view setupSubviews];
    
    return view;
}


- (void)timerAction
{
    second++;
    if (second == 60) {
        second = 0;
        minute++;
    }
    
    if (minute == 60) {
        minute = 0;
        hour++;
    }
    
    NSString *title = [NSString stringWithFormat:@"已等待：%02ld小时%02ld分%02ld秒", hour, minute, second];
    [_timeBtn setTitle:title forState:UIControlStateNormal];
    
}

/// 人工估价
- (void)hintPersonGujia
{
    LLSystemAlertView *alertView = [LLSystemAlertView alertViewWithTitle:@"" message:@"继续让我们的鉴定师为您进行人工估价？" cancelTitle:@"取消" cancelBlock:^{} confirmTitle:@"确定" confirmBlock:^BOOL{
     
        if (self.changeToPersonBlock) {
            self.changeToPersonBlock();
        }
        
        return YES;
    }];
    
    [alertView showInWindowWithBackgoundTapDismissEnable:YES];
}

- (void)setupSubviews
{
    _titleLabel = [UILabel labelWithTitle:self.model.Progress_Text color:UIColorHex(#F0253E) fontSize:16];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(30);
        make.height.mas_equalTo(30*kScale);
    }];
    
    
    
    _timeBtn = [UIButton createButtonWithTitle:@"已等待：" titleColor:UIColorHex(#F0253E) titleFont:[UIFont systemFontOfSize:15] backgroundColor:nil target:nil action:nil];
    [_timeBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    _timeBtn.hidden = YES;
    [self addSubview:_timeBtn];
    [_timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.centerX.bottom.equalTo(self).mas_offset(-10);
    }];
    
    // 获取当前时间
    NSString *currentTimeStr = [LLDateUtilis getCurrentTimeString];
    // 时间差
    NSDateComponents *diff = [LLDateUtilis compareFromDateString:self.model.TriggerArtificialPriceTime toDateString:currentTimeStr deteUnit:Hour | Minute | Second];
    hour = diff.hour >= 0 ? diff.hour : 0;
    minute = diff.minute >= 0 ? diff.minute : 0;
    second = diff.second >= 0 ? diff.second : 0;
    
    NSString *title = [NSString stringWithFormat:@"已等待：%02ld小时%02ld分%02ld秒", hour, minute, second];
    [_timeBtn setTitle:title forState:UIControlStateNormal];
    
    YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:proxy selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    
    NSMutableAttributedString *attr1 = [[NSMutableAttributedString alloc] initWithString:@"自动估价是基于系统钻石售价评估而来，没有考虑磨损、擦伤等因素，价格仅供参考。精准估价您可以进一步选择人工估价"];
    [attr1 addAttribute:NSForegroundColorAttributeName value:UIColorHex(#F0253E) range:NSMakeRange(0, attr1.string.length)];
    [attr1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attr1.string.length)];
    @weakify(self)
    [attr1 setTextHighlightRange:[attr1.string rangeOfString:@"人工估价"] color:UIColorHex(#397EEF) backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        @strongify(self)
        [self hintPersonGujia];
        
    }];
    
    NSString *str3 = [NSString stringWithFormat:@"您提交的估价正在处理中，需要您进一步完善信息\n原因：%@", self.model.Information_Record_EvaluationComment];
    NSMutableAttributedString *attr3 = [[NSMutableAttributedString alloc] initWithString:str3];
    [attr3 addAttribute:NSForegroundColorAttributeName value:UIColorHex(#F0253E) range:NSMakeRange(0, attr3.string.length)];
    [attr3 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attr3.string.length)];
    [attr3 setTextHighlightRange:[attr3.string rangeOfString:@"完善信息"] color:UIColorHex(#397EEF) backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        @strongify(self)
        if (self.perfectInfoBlock) {
            NSString *reason = [NSString stringWithFormat:@"原因：%@", self.model.Information_Record_EvaluationComment];
            self.perfectInfoBlock(reason);
        }
    }];
    // 评语
    NSMutableAttributedString *attr4 = [[NSMutableAttributedString alloc] initWithString:self.model.Information_Record_EvaluationComment];
    [attr4 addAttribute:NSForegroundColorAttributeName value:UIColorHex(#F0253E) range:NSMakeRange(0, attr4.string.length)];
    [attr4 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attr4.string.length)];
    
    _contentLabel = [[YYLabel alloc] init];
    _contentLabel.font = [UIFont systemFontOfSize:14];
    _contentLabel.numberOfLines = 0;
    _contentLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    [self addSubview:_contentLabel];
    
    if (self.model.Progress.intValue == 1) {
        _contentLabel.attributedText = attr1;
    } else if (self.model.Progress.intValue == 3) {
        _contentLabel.attributedText = attr3;
    } else if (self.model.Progress.intValue == 4) {
        _contentLabel.attributedText = attr4;
    }
    
    _contentLabel.hidden = self.model.Progress.intValue == 2;
    _timeBtn.hidden = !_contentLabel.hidden;
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self).mas_offset(-20);
        make.height.mas_equalTo(70*kScale);
    }];
    
    
    
}

@end
